
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pleasure_video/constant/color.dart';
import 'package:flutter_pleasure_video/constant/style.dart';
import 'package:flutter_pleasure_video/data/model/yck_rule_model.dart';
import 'package:flutter_pleasure_video/route/router.dart';
import 'package:flutter_pleasure_video/widget/common/buttom_common.dart';
import 'package:flutter_pleasure_video/widget/diglog/app_dialog.dart';
import 'package:flutter_pleasure_video/widget/diglog/common_dialog.dart';
import 'package:get/get.dart';
import 'package:flutter_pleasure_video/util/extensions/string_extensions.dart';
import 'package:flutter_pleasure_video/util/extensions/widget_extensions.dart';

class YckSourceListPage extends StatelessWidget {

  final SourceValueController _controller=SourceValueController();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SourceValueController>(
      init: _controller,
      initState: _initState,
      dispose: (_){
        "扫码界面dispose".print;
      },
      builder: (ctx){
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("资源"),
          ),
          body: _getBody(context),
        );
      },
    );
  }

  Widget _getBody(BuildContext context){
    return Container(
      child: Column(
        children: [
          Expanded(child: SizedBox(child:_body(context))),
          _bottomBox(),
        ],
      ),
    );
  }


  Widget _body(BuildContext context){
    return Container(
      width: Get.width,
      child: Row(
        children: [
          Expanded(child: SizedBox(child:_getYckListView(context))),
          Expanded(child: SizedBox(child:_getMeSourceListView(context))),
        ],
      ),
    );
  }

  Widget _bottomBox(){
    return Container(
      width: Get.width,
      height: Get.height*0.2,
      decoration: BoxDecoration(
        border: Border(top: BorderSide(width: 1,color: AppColor.textHint)),
      ),
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getTextButtonStyle("扫一扫"),
              _getTextButtonStyle("转换"),
              _getTextButtonStyle("验证"),
            ],
          ),
          SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getTextButtonStyle("清空"),
              _getTextButtonStyle("提交"),
            ],
          ),
        ],
      ),
    );
  }

  Widget _getYckListView(BuildContext context){
    return GetBuilder<SourceValueController>(
      id: _controller.updateListKey,
      builder: (ctx){
        if(ctx.yckModelList.length<1){
          return Container(
            alignment: Alignment.center,
            child: Text("暂未添加资源",style: AppStyle.textHintRegular14),
          );
        }
        return ListView.builder(
          itemCount: ctx.yckModelList.length,
          itemBuilder: (context,index){
            return _getYckListItem(ctx.yckModelList[index],context);
          }
        );
      },
    );
  }

  Widget _getMeSourceListView(BuildContext context){
    return GetBuilder<SourceValueController>(
      id: _controller.updateListKey,
      builder: (ctx){
        if(ctx.yckModelList.length<1){
          return Container(
            alignment: Alignment.center,
            child: Text("暂未添加资源",style: AppStyle.textHintRegular14),
          );
        }
        return ListView.builder(
          itemCount: ctx.yckModelList.length,
          itemBuilder: (context,index){
            return _getYckListItem(ctx.yckModelList[index],context);
          }
        );
      },
    );
  }


  Widget _getYckListItem(YckRuleModel model,BuildContext context){
      return GestureDetector(
        onTap: (){
          AppRouter.toSourceAnalyzePage(model);
        },
        onLongPress: (){
          _itemLongPress(model,context);
        },
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 16,vertical: 8),
          child: Text(model.title),
        ),
      );
  }



  _itemLongPress(YckRuleModel model,BuildContext context){
    AppDialog.showCommonDialog(
        context,
        title: "删除消息",
        info: "确认删除这个资源吗？",
        barrierDismissible: false,
        cancel: AppDialogButton("取消", () {
          "点了取消".print;
          return true;
        }),
        confirm: AppDialogButton("确定", () {
          _controller.removeModel(model);
          return true;
        }));
  }

  //初始化
  _initState(state){
    // AppRouter.toScanQRCodePage((dynamic result){
    //   if(result is String) {
    //     result.dialog;
    //   }else{
    //     _controller.setModelList(result);
    //   }
    // });
  }

  //获取按钮
  Widget _getTextButtonStyle(String title){
    return TextButtonStyle(
        title,
        onTap: _onClickListener(title),
        backgroundColor: AppColor.mainRed.withOpacity(0.8),
        allRadius: 8,
        width: 80,
    );
  }

  _onClickListener(String title){

  }

}

class SourceValueController extends GetxController{
  List<YckRuleModel> yckModelList= [];
  String updateListKey="updateList";

  setModelList(List<YckRuleModel> modelList){
    "modelList:${modelList.length}".print;
    this.yckModelList.clear();
    this.yckModelList.addAll(modelList);
    update([updateListKey]);
  }

  removeModel(YckRuleModel model){
    yckModelList.remove(model);
    update([updateListKey]);
  }
}